package top.tangyh.lamp.care.controller;

import cn.hutool.core.util.StrUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.tangyh.basic.base.R;
import top.tangyh.basic.context.ContextUtil;
import top.tangyh.basic.exception.BizException;
import top.tangyh.basic.interfaces.echo.EchoService;
import top.tangyh.basic.base.controller.SuperController;
import top.tangyh.lamp.care.entity.BCareTemplate;
import top.tangyh.lamp.care.service.BCareContentService;
import top.tangyh.lamp.care.entity.BCareContent;
import top.tangyh.lamp.care.service.BCareTemplateService;
import top.tangyh.lamp.care.vo.query.QueryCareContentByPidAndTimeParam;
import top.tangyh.lamp.care.vo.result.BCareTemplateResultVO;
import top.tangyh.lamp.care.vo.save.BCareContentSaveVO;
import top.tangyh.lamp.care.vo.save.BCareTemplateSaveVO;
import top.tangyh.lamp.care.vo.update.BCareContentUpdateVO;
import top.tangyh.lamp.care.vo.result.BCareContentResultVO;
import top.tangyh.lamp.care.vo.query.BCareContentPageQuery;
import top.tangyh.lamp.common.utils.SecurityUtil;
import top.tangyh.lamp.oauth.biz.OauthUserBiz;
import top.tangyh.lamp.oauth.vo.result.DefUserInfoResultVO;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * 护理记录内容表
 * </p>
 *
 * @author zqb
 * @date 2024-08-02 10:21:07
 * @create [2024-08-02 10:21:07] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/careRecord")
@Tag(name = "系统：护理记录")
public class BCareContentController {
//    extends SuperController<BCareContentService, Long, BCareContent
//            , BCareContentSaveVO, BCareContentUpdateVO, BCareContentPageQuery, BCareContentResultVO>
//    private final EchoService echoService;
//    @Override
//    public EchoService getEchoService() {
//        return echoService;
//    }


    private final OauthUserBiz oauthUserBiz;

    private final BCareContentService bCareContentService;

    private final BCareTemplateService bCareTemplateService;

    /**
     * 添加、修改护理记录
     */
    @PostMapping("/addOrUpdateCareContent")
    @Operation(summary = "添加、修改护理记录")
    public R<Boolean> addOrUpdateCareContent(@RequestBody BCareContentSaveVO bCareContentSaveVO) {
//        JwtUserDto jwtUserDto = (JwtUserDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        UserDto user= jwtUserDto.getUser();
        DefUserInfoResultVO defUserInfoResultVO = oauthUserBiz.getUserById(ContextUtil.getUserId());
        bCareContentSaveVO.setRecordBy(ContextUtil.getUserId() + "");
        bCareContentSaveVO.setRecordName(defUserInfoResultVO.getUsername());


        return R.success(bCareContentService.addOrUpdateCareContent(bCareContentSaveVO));
    }

    /**
     * 根据时间、健康号查询护理记录列表
     */
    @PostMapping("/queryCareContentByPidAndTime")
    @Operation(summary = "根据时间、健康号查询护理记录列表(护理记录模块需要用到)")
    public R<List<BCareContentResultVO>> queryCareContentByPidAndTime(@RequestBody QueryCareContentByPidAndTimeParam queryCareContentByPidAndTimeParam) {

        if (queryCareContentByPidAndTimeParam == null || queryCareContentByPidAndTimeParam.getPatientId() == null || queryCareContentByPidAndTimeParam.getTime() == null) {
            return R.fail("参数错误");
        }
        List<BCareContentResultVO> list = bCareContentService.queryCareContentByPidAndTime(queryCareContentByPidAndTimeParam);

        return R.success(list);
    }

    /**
     * 添加、修改护理记录模板
     */
    @PostMapping("/addOrUpdateCareTemplate")
    @Operation(summary = "添加、修改护理记录模板")
    public R<Object> addOrUpdateCareTemplate(@RequestBody BCareTemplateSaveVO yxCareTemplatePO) {
        return R.success(bCareTemplateService.addOrUpdateCareTemplate(yxCareTemplatePO));
    }

    /**
     * 根据健康号护理记录模板
     */
    @GetMapping("/selectCareTemplateByPid")
    @Operation(summary = "根据健康号护理记录模板")
    public R<List<BCareTemplateResultVO>> selectCareTemplateByPid(@RequestParam String patientId,
                                                                  @RequestParam Integer type) {
        if (patientId == null || StrUtil.isEmpty(patientId)) {
            return R.fail("参数错误");
        }

        return R.success(bCareTemplateService.selectCareTemplateByPid(patientId, type));
    }

    /**
     * 根据健康号护理记录模板
     */
    @GetMapping("/template/list/all")
    @Operation(summary = "查询所有护理记录模板")
    public R<List<BCareTemplateResultVO>> selectCareTemplateAll(@RequestParam Integer type) {
        return R.success(bCareTemplateService.listAllByType(type));
    }

    @GetMapping("/template/delete")
    @Operation(summary = "护理记录模板删除")
    public R<BCareTemplate> delete(@RequestParam Long id) {
        if (null != id) {
            BCareTemplate old = bCareTemplateService.getById(id);

            if (null != SecurityUtil.checkRecordAuth(old.getCreatedBy(), old.getUpdatedBy(), ContextUtil.getUserId())) {
                throw new BizException(SecurityUtil.checkRecordAuth(old.getCreatedBy(), old.getUpdatedBy(), ContextUtil.getUserId()));
            }
        }
        BCareTemplate careTemplatePO = new BCareTemplate();
        careTemplatePO.setId(id);
        careTemplatePO.setIsDelete(1);
        return R.success(bCareTemplateService.updateById(careTemplatePO));
    }


    /**
     * 删除护理记录
     */
    @Operation(summary = "删除护理记录")
    @DeleteMapping(value = "/deleteCareContent")
    public R deleteReocrd(@Parameter(description = "表单id", required = true) @RequestParam("id") Long id) {
        if (null != id) {
            BCareTemplate old = bCareTemplateService.getById(id);
            if(null!=old){
                if (null != SecurityUtil.checkRecordAuth(old.getCreatedBy(), old.getUpdatedBy(), ContextUtil.getUserId())) {
                    throw new BizException(SecurityUtil.checkRecordAuth(old.getCreatedBy(), old.getUpdatedBy(), ContextUtil.getUserId()));
                }
            }

        }
        List<Long> ids = new ArrayList<>();
        ids.add(id);
        bCareContentService.removeByIds(ids);
        return R.success();
    }


}


